/Users/thachakritkomolkhetruck/Desktop/IA_NetBeans_Project copy/src/Prototype/PokemonSet.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package Prototype;
  7 
  8 /**
  9  *
 10  * @author thachakritkomolkhetruck
 11  */
 12 import java.util.ArrayList;
 13 
 14 import java.io.FileWriter;
 15 import java.io.IOException;
 16 
 17 import java.io.File;  // Import the File class
 18 import java.io.FileNotFoundException;  // Import this class to handle errors
 19 import java.util.Scanner; // Import the Scanner class to read text files
 20 import java.util.Arrays;
 21 
 22 
 23 import java.io.FileReader;
 24 import javax.swing.JOptionPane;
 25 
 26 
 27 import org.json.simple.JSONArray;
 28 import org.json.simple.JSONObject;
 29 import org.json.simple.parser.JSONParser;
 30 import org.json.simple.parser.ParseException;
 31 
 32 public class PokemonSet {
 33     private int fieldHp = 100;
 34     private String pokemon;
 35     private String[] moveset = new String[4];
 36     private String item;
 37     private double[] modifier = {1,1,1,1,1};
 38     private String ability;
 39     private Boolean ok = true;
 40 
 41    
 42     private ArrayList<Integer> index;
 43 
 44     public PokemonSet(){}
 45     public PokemonSet(String pokemon, String move1, String move2, String move3, String move4, String ability, String item, String nature, ArrayList<Integer> index){
 46         
 47         this.pokemon = pokemon;
 48         String [] moves = {move1, move2, move3, move4};
 49         this.moveset = moves;
 50         this.index = index;
 51         this.ability = ability;
 52     }
 53     
 54     public PokemonSet( 
 55             String pokemon, String move1, String move2, String move3, String move4, String item, String ability, 
 56             javax.swing.JTextField errorTF, javax.swing.JTextField pokemonTF, javax.swing.JTextField move1TF, javax.swing.JTextField move2TF,
 57             javax.swing.JTextField move3TF, javax.swing.JTextField move4TF, javax.swing.JTextField itemTF, javax.swing.JTextField abilityTF
 58     ){
 59         
 60 
 61         this.pokemon = pokemon;
 62         setOk(true);
 63         String [] moves = {move1, move2, move3, move4};
 64         this.moveset = moves;
 65         this.item = item;
 66         this.ability = ability; 
 67         
 68         
 69 
 70  
 71         JSONParser jsonParser = new JSONParser();
 72          
 73         try (FileReader reader = new FileReader("pokemon.json"))
 74         {
 75             //Read JSON file
 76             Object obj = jsonParser.parse(reader);
 77  
 78             JSONArray employeeList = (JSONArray) obj;
 79 
 80    
 81             //Iterate over employee array
 82             employeeList.forEach( emp ->
 83  
 84                 Fillerpokemon(
 85                     (JSONObject) emp, this.pokemon, this.moveset, this.item, this.ability, errorTF, pokemonTF, move1TF, move2TF, move3TF, move4TF,
 86                         itemTF, abilityTF
 87                 )  
 88             );
 89  
 90         } catch (FileNotFoundException e) {
 91             e.printStackTrace();
 92         } catch (IOException e) {
 93             e.printStackTrace();
 94         } catch (ParseException e) {
 95             e.printStackTrace();
 96         }
 97         if(getOk())
 98         {
 99 //      //System.out.println("error");
100             errorTF.setText("Can't find pokemon!");
101         }else{
102             errorTF.setText("");
103         }
104         
105  
106         
107     }
108     
109     public void Fillerpokemon(JSONObject pokedex, String name, String[] moves, String item, String ability, 
110             javax.swing.JTextField errorTF, javax.swing.JTextField pokemonTF, javax.swing.JTextField move1TF, javax.swing.JTextField move2TF,
111             javax.swing.JTextField move3TF, javax.swing.JTextField move4TF, javax.swing.JTextField itemTF, javax.swing.JTextField abilityTF
112     )
113             
114     {   
115         JSONObject pokemonName = (JSONObject) pokedex;
116         String pokeName = (String) pokemonName.get("name");
117 //        //System.out.println(pokemonName.get("name"));
118           
119         //Get employee object within list
120 //        JSONObject pokemonName = (JSONObject) pokedex.get("name");
121 //        //System.out.println(pokemonName.get("name").equals(name));
122 
123         if(pokeName.equalsIgnoreCase(name)){
124                
125             try {  
126                 FileWriter myWriter = new FileWriter("./src/Filedata/"+ name + ".txt");
127     
128 //                   //System.out.println(pokemonName.get("name"));
129 //                   //System.out.println(pokemonName.get("types"));
130 //                   //System.out.println(pokemonName.get("base_stats"));
131                 myWriter.write(name);
132                 myWriter.write("\n"+ Arrays.toString(moves));
133                 myWriter.write("\n"+ item);
134                 myWriter.write("\n"+ ability);
135                 myWriter.write("\n"+ pokemonName.get("types"));
136                 myWriter.write("\n"+ pokemonName.get("base_stats"));
137                 myWriter.write("\n"+ pokedex.get("type"));
138 //                
139 //                JSONObject pokemonBase = (JSONObject)pokedex.get("base");
140 //       
141 //                myWriter.write("\n"+pokemonBase.get("HP"));
142 //                myWriter.write("\n"+ pokemonBase.get("Attack"));
143 //                myWriter.write("\n"+ pokemonBase.get("Defense"));
144 //                myWriter.write("\n"+ pokemonBase.get("Sp. Attack"));
145 //                myWriter.write("\n"+ pokemonBase.get("Sp. Defense"));
146 //                myWriter.write("\n"+ pokemonBase.get("Speed"));
147                 myWriter.close();
148 //
149                 JOptionPane.showMessageDialog(null, "Add Pokemon success!");
150                 pokemonTF.setText("");
151                 move1TF.setText("");
152                 move2TF.setText("");
153                 move3TF.setText("");
154                 move4TF.setText("");
155                 itemTF.setText("");
156                 abilityTF.setText("");
157                 errorTF.setText("..");
158                 setOk(false);
159                 
160                 
161   
162               } catch (IOException e) {
163                 //System.out.println("An error occurred.");
164                 e.printStackTrace();
165               }
166             
167         }
168 
169         
170     }
171     
172 
173               
174     public void insertModifiers(double mod1, double mod2, double mod3, double mod4, double mod5){
175         double[] modArray = {mod1, mod1, mod3, mod4, mod5}; 
176         this.modifier = modArray;
177     }
178 
179     public ArrayList<Integer> getIndex() {
180         return index;
181     }
182     public Boolean getOk(){
183         return ok;
184     }
185     public void setOk(Boolean ok){
186         this.ok=ok;
187     }
188     public String getAbility(){
189         return ability;
190     }
191 
192 
193     public double[] getModifier(){
194         return modifier;
195     }
196     public int getFieldHp(){
197         return fieldHp;
198     }
199     public void setFieldHp(int fieldHp){
200         this.fieldHp = fieldHp;
201     }
202 
203     public String[] getMoveset() {
204         return moveset;
205     }
206    
207 
208 
209 
210     public String getItem() {
211         return item;
212     }
213 
214 
215 
216 
217 
218     public void setItem(String item) {
219         this.item = item;
220     }
221    
222 
223     public void setIndex(ArrayList<Integer> index) {
224         this.index = index;
225     }
226     public void setModifier(double[] modifier){
227         this.modifier = modifier;
228     }
229 
230     public void setMoveset(String[] moveset) {
231         this.moveset = moveset;
232     }
233 
234     public void setPokemon(String pokemon) {
235         this.pokemon = pokemon;
236     }
237     public void setAbility(String ability){
238         this.ability = ability;
239     }
240 
241 
242    
243 }
244